
package basics.day06.first;
import java.util.ArrayList;
import java.util.Scanner;

public class Shopping {
    // 入口文件
    public static void main(String[] args) {
        // 创建存储商品的集合
        ArrayList<Goods> goodsArr = new ArrayList<Goods>();
        // 添加商品
        addGoods(goodsArr);

        while (true) {
            int chooseNum = chooseFunction();
            switch (chooseNum) {
                case 1:
                    // 展示所有的商品
                    printAllGoods(goodsArr);
                    break;
                case 2:
                    updateGoods(goodsArr);
                    break;
                case 3:
                    return;
                default:
                    System.out.println("输入有误，请重新输入");
                    break;
            }
        }
    }

    // 修改库存
    public static void updateGoods(ArrayList<Goods> arr) {
        for (int i = 0; i < arr.size(); i++) {
            // 获取商品
            Goods g = arr.get(i);
            System.out.println(g.brand + "的库存：" + g.count);
            // 创建键盘输入
            Scanner s = new Scanner(System.in);
            System.out.println("请输入" + g.brand + "的库存：");
            int count = s.nextInt();
            g.count = count;
        }
    }

    // 展示操作选项
    public static int chooseFunction() {
        System.out.println("------------库存管理------------");
        System.out.println("1. 查看库存清单");
        System.out.println("2. 修改商品库存数量");
        System.out.println("3. 退出");

        // 创建键盘输入
        Scanner s = new Scanner(System.in);
        int i = s.nextInt();
        return i;
    }

    // 展示所有的商品
    public static void printAllGoods(ArrayList<Goods> arr) {
        // 存储总库存
        int totalCount = 0;
        // 存储总价格
        double totalMoney = 0;
        System.out.println("------------商品清单------------");
        System.out.println("品牌名称    单价    尺寸    库存");
        for (int i = 0; i < arr.size(); i++) {
            Goods g = arr.get(i);
            System.out.println(g.brand + "   " + g.price + "    " + g.size + "    " + g.count);
            totalCount += g.count;
            totalMoney += g.price * g.count;
        }
        System.out.println("总库存：" + totalCount);
        System.out.println("总价格：" + totalMoney);
    }

    // 添加商品
    public static void addGoods(ArrayList<Goods> arr) {
        Goods g1 = new Goods();
        g1.brand = "Macbook";
        g1.price = 9999;
        g1.count = 10;
        g1.size = 15;
        arr.add(g1);

        Goods g2 = new Goods();
        g2.brand = "Thinkpad";
        g2.price = 8888;
        g2.size = 13.5;
        g2.count = 5;
        arr.add(g2);
    }
}